"""
Case Type   : borrow_work_mem
Case Name   : gs_guc set方式设置borrow_work_mem参数
Create At   : 2025/4/10
Owner       : @li-xin12345
Description :
    1、设置相关参数有效值
    htap_borrow_mem_percent使用默认值
    max_imcs_cache使用默认值
    lmemfabric_client_path依赖rackmanager,当前使用默认值，不影响参数设置
    alter system set max_borrow_memory to '1024MB';
    2、设置 borrow_work_mem='300MB'、设置 borrow_work_mem='1kB'、
    设置 borrow_work_mem='1GB'
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BorrowMemTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_db_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.param_names = ['htap_borrow_mem_percent', 'max_imcs_cache',
                            'max_borrow_memory', 'lmemfabric_client_path',
                            'borrow_work_mem']
        self.default_values = [self.common.show_param(i) for i in
                               self.param_names]
        self.param_values = ['300MB', '1kB', '1GB']

    def test_borrow_mem(self):
        text = '-----step1:设置相关参数有效值;expect:成功-----'
        self.log.info(text)
        res = self.pri_db_sh.execut_db_sql(
            f'alter system set {self.param_names[2]} to \'1024MB\';')
        self.log.info(res)
        self.assertIn(self.constant.alter_system_success_msg, res,
                      '执行失败：' + text)
        res = self.pri_db_sh.restart_db_cluster()
        self.log.info(res)
        status = self.pri_db_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)

        res = self.common.show_param(self.param_names[2])
        self.log.info(res)
        self.assertEqual('1GB', res, '执行失败：' + text)

        text = ("-----step2:设置 borrow_work_mem='300MB'、"
                "设置 borrow_work_mem='1kB'、"
                "设置 borrow_work_mem='1GB';expect:成功-----")
        self.log.info(text)
        for i in self.param_values:
            res = self.pri_db_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'{self.param_names[4]}={i}')
            self.log.info(res)
            self.assertTrue(res, "执行失败：" + text)
            res = self.pri_db_sh.restart_db_cluster()
            self.log.info(res)
            status = self.pri_db_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in status)
            res = self.common.show_param(self.param_names[4])
            self.log.info(res)
            self.assertEqual(i, res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step3:清理环境; expect:成功-----'
        self.log.info(text)
        current_values = [self.common.show_param(i) for i in self.param_names]
        self.log.info(current_values)
        reset_flag = False
        if current_values != self.default_values:
            for param_name, current_val, default_val in zip(
                    self.param_names, current_values, self.default_values):
                if current_val != default_val:
                    set_res = self.pri_db_sh.execute_gsguc(
                       'set', self.constant.GSGUC_SUCCESS_MSG,
                        f'{param_name}={default_val}')
                    self.log.info(set_res)
                    self.assertTrue(set_res, '执行失败：' + text)
                    reset_flag = True
        if reset_flag:
            res = self.pri_db_sh.restart_db_cluster()
            self.log.info(res)
            status = self.pri_db_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in status)
            res = [self.common.show_param(i) for i in self.param_names]
            self.log.info(res)
            self.assertEqual(self.default_values, res, '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')